﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using MyFloridaHouse.libSvUtilities.CustomColors;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace MyFloridaHouse.libSvOpenDistricts.Districts
{
    public delegate void DistrictColorConditionsChangedEventHandler();

    public class DistrictColors :INotifyPropertyChanged
    {

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region DistrictColorConditionsChanged
        public static event DistrictColorConditionsChangedEventHandler DistrictColorConditionsChanged;
        public void RaiseDistrictColorConditionsChanged(object sender)
        {
            if (DistrictColorConditionsChanged != null)
            {
                DistrictColorConditionsChanged();
            }
        }
        #endregion

        public DistrictColors(string colorType)
        {
            // set up a globally available reference
            _districtColorsInstance = this;

            if (colorType == "PREDEFINED")
                districtColors = new PreDefinedColors();
            else 
                //(colorType == "WEBSAFE")
                districtColors = new WebSafeColors();
        }


        private static DistrictColors _districtColorsInstance;
        public static DistrictColors districtColorsInstance
        {
            get { return _districtColorsInstance; }
            set { _districtColorsInstance = value; }
        }

        private int _opacity = 80;
        public int opacity 
        {
            get { return _opacity; }
            set
            {
                _opacity = value;
                NotifyPropertyChanged("opacity");
                RaiseDistrictColorConditionsChanged(this);
            }
        }

        private ICustomColors districtColors;

        public static SolidColorBrush GetBrushFromIndex(int districtIndex)
        {
            if (DistrictColors.districtColorsInstance == null)
            {
                return new SolidColorBrush(Colors.Magenta);
            }
            if (DistrictColors.districtColorsInstance.districtColors == null)
            {
                return new SolidColorBrush(Colors.Orange);
            }
            return new SolidColorBrush(DistrictColors.districtColorsInstance.districtColors.GetColorFromIndex(districtIndex, DistrictColors.districtColorsInstance.opacity/100.0));
        }

        public static Color GetColorFromIndex(int districtIndex)
        {
            return DistrictColors.districtColorsInstance.districtColors.GetColorFromIndex(districtIndex, DistrictColors.districtColorsInstance.opacity / 100.0);
        }

        public static Color GetSolidColorFromIndex(int districtIndex)
        {
            return DistrictColors.districtColorsInstance.districtColors.GetColorFromIndex(districtIndex, 1.0);
        }

    }
}
